package webService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Stack;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import util.DBUtil;

public class WebService {
	public static void main(String[] args) throws Exception {
		while(true){
			ServerSocket server = new ServerSocket(9000);
			Socket sk = server.accept();
			BufferedReader reader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
			OutputStream out = sk.getOutputStream();
			
			String line = null;
			while((line=reader.readLine()).trim().length() != 0){
				System.out.println(line);
			}
			System.out.println("header end...");
			
			Document dom = new Document();
			Element root = new Element("html");
			Element body = new Element("body");
			DBUtil.loadDataForWeb();
			String buf[] = DBUtil.loadDataForWeb().split("\\\\");
			
			for(String i:buf){
				System.out.println(i);
				Element a3 = new Element("li");
				a3.setText(i);
				body.addContent(a3);
			}
			Element a4 = new Element("li");
			a4.setText("..........................................................");
			body.addContent(a4);
			
			Element a1 = new Element("a");
			a1.setText("start");
			a1.setAttribute("href", "http://localhost:8887/start");
			body.addContent(a1);
			
			Element a5 = new Element("li");
			a5.setText("..........................................................");
			body.addContent(a5);
			
			Element a2 = new Element("a");
			a2.setText("stop");
			a2.setAttribute("href", "http://localhost:8887/stop");
			body.addContent(a2);
			
			root.addContent(body);
			dom.setRootElement(root);
			XMLOutputter ot = new XMLOutputter();
			
			out.write("HTTP/1.1 200 OK\r\n\r\n".getBytes());
			ot.output(root, out);
			
			out.close();
			reader.close();
			out.close();
			sk.close();
			server.close();
		}
	}
}
